package com.face.gaoml.netlibrary.module;

import android.app.Application;

import com.face.gaoml.netlibrary.data.cookies.CommonInterceptor;
import com.face.gaoml.netlibrary.data.cookies.GetCookiesInterceptor;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 网络 模块 实现 了  网络 对象的提供 ，子类 提供 自己 封装的 网络请求 就可以，还有 地址
 * Created by gml on 17/1/6.
 */
@Module
public class AppModule {

    private Application application;

    public AppModule(Application application) {
        this.application = application;
    }


    @Provides
    @Singleton
    Application provideApplicationContext() {
        return application;
    }


    //    @Provides
//    @Singleton
//    ThreadExecutor provideThreadExecutor(JobExecutor jobExecutor) {
//        return jobExecutor;
//    }
//
//    @Provides
//    @Singleton
//    ApiService providesApiService(RetrofitManager retrofitManager) {
//        return retrofitManager.getService();
//    }
//
//    @Provides
//    @Singleton
//    SpfManager provideSpfManager() {
//        return new SpfManager(application);
//    }
//
//    @Provides
//    public MsgAlert provideToastUtil(Application app) {
//        return new MsgAlert(app);
//    }

/**************************************///分割线
    //    String baseUrl = "http://t.jiemian.com:10000/";
//    String baseUrl = "http://192.168.1.5:8080"; ／／子类去实现  对URL 的控制

    /**
     * 集中处理 公共请求字段
     */
    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient(CommonInterceptor commonInterceptor, GetCookiesInterceptor cookiesInterceptor
    ) {
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(commonInterceptor)
                .addInterceptor(cookiesInterceptor)
                .build();
        return client;
    }


    //构造方法私有
    @Provides
    @Singleton
    Retrofit provideRetrofit(OkHttpClient okHttpClient, RxJavaCallAdapterFactory callAdapterFactory, GsonConverterFactory gsonConverterFactory ,String getUrl) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(getUrl)
                .addConverterFactory(gsonConverterFactory)
                .addCallAdapterFactory(callAdapterFactory)
                .client(okHttpClient)
                .build();
        return retrofit;
    }

//    @Provides
//    @Singleton
//    ApiService provideApi(Retrofit retrofit) {
//        return retrofit.create(ApiService.class);
//    }


    @Singleton
    @Provides
    protected RxJavaCallAdapterFactory getCallAdapterFactory() {
        return RxJavaCallAdapterFactory.create();
    }

    @Singleton
    @Provides
    protected GsonConverterFactory getGsonConvertFactory() {
        return GsonConverterFactory.create();
    }


    @Singleton
    @Provides
    protected GetCookiesInterceptor getGetCookiesInterceptor() {
        return new GetCookiesInterceptor();
    }

//    @Provides
//    @Singleton
//    Util provideUtil(Application application) {
//        return new Util(application);
//    }



}
